package org.alex.netShop.pages;



import org.alex.netShop.interfaces.Product;
import org.alex.netShop.interfaces.Products;
import org.alex.netShop.interfaces.User;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

public class ViewProduct
{
	@Inject
	@Property
	private Products products;
	@InjectPage
	private ProductDetails productDetailsPage;
	@Property
	private Product currentProduct;
	@InjectPage
	private AddPurchase addPurchase;
	
	@OnEvent (component="productDetailsLink")
	Object onShowDetails(String name) {
		Product chosenProduct = products.retrieveProduct(name);
		productDetailsPage.setChosenProduct(chosenProduct);
		return productDetailsPage;
	}
	@OnEvent(component= "addProductLink")
	Object onProductLink(String name){
		addPurchase.setPurchaseProduct(name);
		return addPurchase;
	}

	@OnEvent(component="removeProductLink")
	Object onRemoveLink(String name)
	  {
		  products.retrieveProduct(name);
		  products.removeProduct(name);
			return this;
	  }
	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		return null;
	}

}
